package com.hnust.service.impl;

import com.hnust.dao.CollegeDao;
import com.hnust.dao.TeacherDao;
import com.hnust.domain.College;
import com.hnust.domain.Teacher;
import com.hnust.domain.record.LoginData;
import com.hnust.service.TeacherService;
import com.hnust.util.IDUtil;
import com.hnust.util.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("teacherServer")
public class TeacherImpl implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private CollegeDao collegeDao;

    // 验证Token的时候根据id和password查询是否存在这个账号
    @Override
    public int findTeacherByIdPass(String id, String password) {
        return teacherDao.findTeacherByIdPass(id, password);
    }

    /**
     * 老师登录
     * @param faculty_number
     * @param password
     * @return
     */
    @Override
    public LoginData login(String faculty_number, String password) throws Exception {
        if(teacherDao.findTeacherByNumberPass(faculty_number,password)==1){
            LoginData loginData=new LoginData();
            Teacher teacher=teacherDao.getTeacherById(faculty_number);
            loginData.setToken(JwtToken.createToken(teacher.getTeaId(),teacher.getPassword()));
            loginData.setTeacher_name(teacher.getName());
            loginData.setCollege_id(teacher.getColId());
            loginData.setTeacher_id(teacher.getTeaId());
            loginData.setCollege_name(collegeDao.getCollegeNameById(teacher.getColId()));
            return loginData;
        }else {
            return null;
        }
    }

    @Override
    public String register(String name, String number, String password, String phone, String email,String collegeId) {
        //System.out.println(email);
        if(teacherDao.getTeacherById(number)!=null){
            return "注册失败，该工号已经被注册了";
        }else if(teacherDao.getTeacherByPhone(phone)!=null){
            return "注册失败，该手机号已经被注册了";
        }else{
            if(teacherDao.insertTeacher(IDUtil.getPrimaryKey(), name, number, password, phone, email,collegeId))
                return "注册成功";
            else
                return "注册出现异常";
        }
    }

    @Override
    public List<College> getAllCollege() {
        return collegeDao.getAllCollege();
    }
}
